function searchBST(bt, key, q) {
    if (!bt) return { code: 0, node: q}
    if (bt.data === key) return {code: 1, node: bt, parent: q}
    if (key < bt.data) {
        return searchBST(bt.leftChild, key, bt)
    } else {
        return searchBST(bt.rightChild, key, bt)
    }
}
 
 /**
  * 二叉排序树的删除算法
  * @param {*} bt 
  * @param {*} key 
  */
 function deleteBST(bt, key) {
       let p, f, s, q, res
       res = searchBST(bt, key, null)
       if (res.code === 0) return 0
       p = res.node
       f = res.parent
       if (!(p.rightChild)) {
            if (f.leftChild === p) {
                f.leftChild = p.leftChild
            } else {
                f.rightChild = p.leftChild
            }
       } else {
           if (!(p.leftChild)) {
               if (f.leftChild === p) {
                   f.leftChild = p.rightChild
               } else {
                   f.rightChild = p.rightChild
               }
           } else {
               q = p
               s = p.leftChild
               while (s.rightChild) { q = s; s = s.rightChild }
               q.data = s.data
               if (q != p) {
                   q.rightChild = s.leftChild
               } else {
                   q.leftChild = s.leftChild
               }
           }
       }
       
 }